package org.cocos2dx.javascript;
import android.content.Context;
import android.util.Log;

import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
public class WDNativePlatform extends AppActivity {

    private String TAG = Config.TAG;
    private static WDNativePlatform instance;
    public AppActivity app = null;
    private Context context = null;
    private String jsStr = "";
    private static String androidId = "";
    private WDNativePlatform () {}
    public static WDNativePlatform getInstance() {
        if (instance == null) {
            instance = new WDNativePlatform();
        }
        return instance;
    }
    public void init (AppActivity _app) {
        app = _app;
    }
    public static  void customEvent(String event){
        Log.d(Config.TAG, "customEvent"+event);
        //==========>customEvent
    }
    public static void onExit() {
        OPGameActivity.getInstance().exit();
    }
    public static void login() {
        Log.d(Config.TAG, "==========login==============");
        OPGameActivity.getInstance().doLogin();
    }
   
    public static void showAd() {
        OPGameActivity.getInstance().playVideo();
    }

    public static void showBannerAd() {
        Log.d(Config.TAG, "static==========showBannerAd");
        OPGameActivity.getInstance().showBannerAd();
    }
    public static void hideBannerAd() {
        OPGameActivity.getInstance().hideBannerAd();
    }
    public static void showFullVideo() {

    }
    public static void showFeedAd() {

    }
    public static void hideFeedAd() {

    }
    public static void createNativeAd(){
        Log.d(Config.TAG, "static==========createNativeAd");
        OPGameActivity.getInstance().showNativeAd();
    }
    public void loginFinish (Map result) {
        Log.d(Config.TAG, "loginFinish: wdnativeplatform");
        JSONObject jsonObj = new JSONObject(result);
        String loginData = jsonObj.toString();
        System.out.println("native login finish" + loginData);
        jsStr = "window.wdNativeLoginResult('" + loginData + "')";
        this.callJS();
    }
    public void videoFinish (Map result) {
        JSONObject jsonObj = new JSONObject(result);
        String videoData = jsonObj.toString();
        System.out.println("video ad finish" + videoData);
        jsStr = "window.wdNativeVideoResult('" + videoData + "')";
        this.callJS();
    }
    public void test (Map result) {
        String payData = "test";
        System.out.println("native test" + payData);
        jsStr = "window.wdNativeTest('" + payData + "')";
        this.callJS();
    }

    public void callJS () {
        app.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString(jsStr);
            }
        });
    }
    public static void register(){

    }

    public static void loginSuccess(String jsStr){
        Log.d(Config.TAG, "LoginSuccess"+jsStr);
        String[] splitStr=jsStr.split("&");
        OPGameActivity.getInstance().sendRoleInfo(1,splitStr[1],splitStr[0]);
    }

}
